package com.cooba.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.content.ContextCompat;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;


import com.cooba.views.loadview.LoadViewHelper;

import java.io.File;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2015/12/11.
 * 手机号判断
 */
public class Utils {
    protected static LoadViewHelper helper;

    //后期升级判断手机号
    public static boolean isnumber(String s) {
        Pattern p = Pattern.compile("1\\d{10}");
        Matcher m = p.matcher(s);
        boolean b = m.matches();
        return b;
    }

    public static String replaceString(String title) {
        String title1 = title.replaceAll("&ldquo;", "\"");
        String title2 = title1.replaceAll("&rdquo;", "\"");
        String title3 = title2.replaceAll("\\\\", "");
        return title3;
    }

    public static String replace(String content) {
        String str = content.replaceAll("&lt;", "<").replaceAll("&gt;", ">")
                .replaceAll("&amp;", "&").replaceAll("&nbsp;", " ")
                .replaceAll("&quot;", "\"").replaceAll("&hellip;","...")
                .replaceAll("&ldquo;", "\"").replaceAll("&rdquo;", "\"");

        return str;
    }

    public static String htmlFilter(String content){
        String regEx_html = "<[^>]+>"; //定义HTML标签的正则表达式
        Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
        Matcher m_html = p_html.matcher(content);
        content = m_html.replaceAll(""); //过滤html标签
        return content;
    }


    /**
     * 字符串日期截取
     *
     * @param str
     * @return date
     */
    public static String subStrTime(String str) {
        int len = str.length();
        String time = null;
        if (len >= 16) {
            time = str.substring(0, 16);
        } else if (len >= 10) {
            time = str.substring(0, 10);
        } else {
            time = str;
        }


        return time;
    }

    /**
     * 设置颜色大小
     *
     * @param ctx
     * @param text
     * @param start
     * @param end
     * @param colorResId
     * @param size       单位dp
     * @return
     */
    public static SpannableString getColorSize(Context ctx, CharSequence text, int start, int end, int colorResId,
                                               int size) {
        SpannableString sString = new SpannableString(text);
        sString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(ctx, colorResId)), start, end,
                Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        sString.setSpan(new AbsoluteSizeSpan(size, true), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return sString;
    }

    /**
     * 格式化单位
     *
     * @param size
     * @return
     */
    public static String FormatSize(double size) {
        double kiloByte = size / 1024;
        if (kiloByte < 1) {
//            return size + "Byte";
            return "0K";
        }

        double megaByte = kiloByte / 1024;
        if (megaByte < 1) {
            BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
            return result1.setScale(2, BigDecimal.ROUND_HALF_UP)
                    .toPlainString() + "KB";
        }

        double gigaByte = megaByte / 1024;
        if (gigaByte < 1) {
            BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
            return result2.setScale(2, BigDecimal.ROUND_HALF_UP)
                    .toPlainString() + "MB";
        }

        double teraBytes = gigaByte / 1024;
        if (teraBytes < 1) {
            BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
            return result3.setScale(2, BigDecimal.ROUND_HALF_UP)
                    .toPlainString() + "GB";
        }
        BigDecimal result4 = new BigDecimal(teraBytes);
        return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString()
                + "TB";
    }
    /**
     * @param view
     *            需要过载动画覆盖的view
     */
    public static LoadViewHelper initLoadViewHelper(View view) {
        helper = new LoadViewHelper(view);
        return helper;
    }
}
